package top.zhangjianyong.tools.service.api.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.Guaguale;
import top.zhangjianyong.tools.factory.GuagualeFactory;
import top.zhangjianyong.tools.service.api.GuagualeService;
import top.zhangjianyong.tools.service.mybatis.IGuagualeService;
import top.zhangjianyong.tools.utils.ApiResponse;

import java.util.List;

/**
 * @author ： zhangjianyong
 * @date ： 2023/3/13 9:20 AM
 */
@Slf4j
@Service
public class GuagualeServiceImpl implements GuagualeService {

    @Autowired
    private IGuagualeService iGuagualeService;

    @Override
    public ApiResponse listLimit(int limit) {
        List<Guaguale> guaguales = iGuagualeService.listLimit(limit);
        return ApiResponse.success(guaguales);
    }

    @Override
    public ApiResponse save(String type, String code,int ticketPrice, int bonus) {
        log.info("保存呱呱：" + type + "，" + code + "，"+ticketPrice+"," + bonus);
        code = code.trim();
        Guaguale guaguale = GuagualeFactory.create(type, code,ticketPrice, bonus);
        Guaguale one = iGuagualeService.qryByTypeAndCode(type, code);
        if (one != null) {
            guaguale.setId(one.getId());
            guaguale.setDelFlag(0);
            iGuagualeService.updateById(guaguale);
        } else {
            iGuagualeService.save(guaguale);
        }

        return ApiResponse.success();
    }

    @Override
    public ApiResponse del(Long id) {
        iGuagualeService.del(id);
        return ApiResponse.success();
    }
}
